package com.hellobike.haxing.modules.purchase.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.hellobike.haxing.modules.purchase.entity.TPurchasePro;
import com.hellobike.haxing.modules.supplier.dto.ProductSoldDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: 商品列表
 * @Author: haxing-boot
 * @Date:   2020-01-20
 * @Version: V1.0
 */
public interface TPurchaseProMapper extends BaseMapper<TPurchasePro> {

    @Update("UPDATE t_purchase_pro SET supplier_price = #{supplierPrice}, supply_volume = #{supplyVolume},update_time = now()" +
            " WHERE sku_id = #{skuId} ")
    void updatePurchaseProFromClp(@Param("skuId") Long skuId, @Param("supplierPrice") BigDecimal supplierPrice,@Param("supplyVolume") Integer supplyVolume);


    @Update("UPDATE t_purchase_pro SET " +
            "sku_name =  #{skuName}, " +
            "dimension_values = #{dimensionValues}, " +
            "sku_unit = #{skuUnit}, " +
            "barcode = #{barcode}, " +
            "date_produce = #{dateProduce}, " +
            "date_due = #{dateDue}, " +
            "origin = #{origin}, " +
            "update_time = now() " +
            "WHERE sku_id = #{skuId}")
    void updatePurchaseProFromSku(@Param("skuId") Long skuId, @Param("skuName") String skuName,@Param("dimensionValues") String dimensionValues,@Param("skuUnit") String skuUnit,@Param("dateProduce") String dateProduce,@Param("barcode") String barcode,@Param("dateDue") String dateDue,@Param("origin") String origin);

    @Update("UPDATE t_purchase_pro SET purchase_volume = purchase_volume + #{purchaseVolume}, is_order = #{isOrder} , missing_parts_volume = #{missingPartsVolume} , is_missing_parts = #{isMissingParts} " +
            " WHERE id = #{id} ")
    void updatePurchaseByAdd(@Param("id") Long id, @Param("purchaseVolume") Integer purchaseVolume,@Param("isOrder") Integer isOrder,@Param("missingPartsVolume") Integer missingPartsVolume,@Param("isMissingParts") Integer isMissingParts);

    @Update("UPDATE t_purchase_pro SET purchase_volume = purchase_volume - #{purchaseVolume}, is_order = #{isOrder} , missing_parts_volume = #{missingPartsVolume} , is_missing_parts = #{isMissingParts} " +
            " WHERE id = #{id} ")
    void updatePurchaseBySub(@Param("id") Long id, @Param("purchaseVolume") Integer purchaseVolume,@Param("isOrder") Integer isOrder,@Param("missingPartsVolume") Integer missingPartsVolume,@Param("isMissingParts") Integer isMissingParts);

    @Update("UPDATE t_purchase_pro SET supplier_id = #{supplierId}, supplier_name = #{supplierName},warehouse_id = #{warehouseId}, warehouse_name = #{warehouseName}, supplier_price = #{supplierPrice},supply_volume = #{supplyVolume},update_time = now()" +
            " WHERE sku_id = #{skuId} ")
    void updatePurchaseProFromRecord(@Param("skuId") Long skuId, @Param("supplierId") Long supplierId,@Param("supplierName") String supplierName,@Param("warehouseId") Long warehouseId,@Param("warehouseName") String warehouseName,@Param("supplierPrice") BigDecimal supplierPrice,@Param("supplyVolume") Integer supplyVolume);


    @Select("SELECT od.sku_id, od.product_id AS spu_id, p.title, od.dimension_values, sku.barcode\n" +
            "\t, tp.supplier_name, SUM(num) AS sold_count, SUM(product_amount) AS sold_amount\n" +
            "\t, SUM(num * COALESCE(tp.purchase_price, 0)) AS purchase_amount\n" +
            "FROM t_product p\n" +
            "\tJOIN t_spu ts ON ts.id = p.id\n" +
            "\tJOIN t_order_detail od ON od.product_id = p.id\n" +
            "\tJOIN t_sku sku ON sku.id = od.sku_id\n" +
            "\tJOIN t_order o\n" +
            "\tON o.status NOT IN (2, 7)\n" +
            "\t\tAND o.id = od.order_id\n" +
            "\t\tAND o.success_time = current_date + INTERVAL '23 hour'\n" +
            "\tLEFT JOIN t_purchase_pro tp ON tp.sku_id = od.sku_id\n" +
            "GROUP BY 1, 2, 3, 4, 5, 6")
    List<ProductSoldDto> selectProductSold();

    @Select("select distinct  supplier_id,supplier_name,sku_id,sku_name,supply_volume ,is_missing_parts ,warehouse_id,warehouse_name  from t_purchase_pro tpp  ${ew.customSqlSegment}" +
            "")
    List<TPurchasePro> selectResult(@Param(Constants.WRAPPER) QueryWrapper<TPurchasePro> queryWrapper);

    @Select("select\n" +
            "\tdistinct tpp.supplier_id,\n" +
            "\ttpp.supplier_name,\n" +
            "\ttpp.sku_id,\n" +
            "\ttpp.sku_name,\n" +
            "\ttscp.supply_volume ,\n" +
            "\ttpp.is_missing_parts ,\n" +
            "\ttpp.warehouse_id,\n" +
            "\ttpp.warehouse_name\n" +
            "from\n" +
            "\tt_purchase_pro tpp\n" +
            "join t_supplier_cl_pro tscp on\n" +
            "\ttpp.sku_id = tscp.sku_id\n" +
            "\tand tpp.is_order  = 0\n" +
            "\tand tpp.supplier_id  = tscp.supplier_id \n" +
            "\tand tscp.supply_volume  > 0\n" +
            "\tand tpp.purchase_volume > 0\n" +
            "\tand tpp.warehouse_id  =  #{wareHouseId} \n" +
            "\tand tpp.supply_volume  > 0\n" +
            "\tand tpp.create_time between #{yesterday} and #{today} AND tpp.supplier_id=#{supplierId}")
    List<TPurchasePro> selectSupplierPro(@Param("supplierId") Long supplierId, @Param("wareHouseId") Long wareHouseId, @Param("today") LocalDateTime today, @Param("yesterday") LocalDateTime yesterday);
}
